package com.suwish.pc.ui.component.list;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.android.ddmlib.FileListingService;
import com.android.ddmlib.FileListingService.FileEntry;
import com.suwish.pc.util.ResourceUtils;

/**
 * 
 * @author Min
 *
 */
public class FileListCellEntry extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private FileEntry fileEntry = null;
	
	private JLabel iconLabel = null;
	private JLabel nameLabel = null;
	private JLabel permissLabel = null;
	private JLabel sizeLabel = null;
	private JLabel dateLabel = null;
	private JLabel timeLabel = null;
//	private JLabel ownerLabel = null;
//	private JLabel groupLabel = null;
	
	public FileListCellEntry(){
		this(null);
	}
	public FileListCellEntry(FileEntry fileEntry){
		setOpaque(true);
		this.fileEntry = fileEntry;
		initUI();		
		initData();
	}
	
	private void initData(){
		if(fileEntry == null) return;
		
		iconLabel.setIcon(getIcon(fileEntry));
		nameLabel.setText(fileEntry.getName());
		permissLabel.setText(fileEntry.getPermissions());
		sizeLabel.setText(fileEntry.getSize());
		dateLabel.setText(fileEntry.getDate());
		timeLabel.setText(fileEntry.getTime());
		nameLabel.setToolTipText(fileEntry.getInfo());
	}
	/**
	 * 
	 * @param fileEntry
	 */
	public void reFresh(FileEntry fileEntry){
		setFileEntry(fileEntry);
		initData();
		invalidate();
        validate();
        repaint();
	}
	private void initUI(){
		setLayout(new GridBagLayout());
		iconLabel = new JLabel();
		add(iconLabel, new GridBagConstraints(0, 0, 1, 1, 0, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 10, 0, 0), 0, 0));
		nameLabel = new JLabel();//名称尽量填充长度
		nameLabel.setFont(new Font(Font.DIALOG, Font.PLAIN , 15));
		add(nameLabel, new GridBagConstraints(1, 0, 100, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 0), 0, 0));

		permissLabel = new JLabel();//横向空格数量，大于100 + 1就行了
		add(permissLabel, new GridBagConstraints(200, 0, 1, 1, 0, 1, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(0, 20, 0, 0), 0, 0));

		sizeLabel = new JLabel();
		add(sizeLabel, new GridBagConstraints(201, 0, 1, 1, 0, 1, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(0, 20, 0, 0), 0, 0));

		dateLabel = new JLabel();
		add(dateLabel, new GridBagConstraints(202, 0, 1, 1, 0, 1, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(0, 20, 0, 0), 0, 0));

		timeLabel = new JLabel();
		add(timeLabel, new GridBagConstraints(203, 0, 1, 1, 0, 1, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(0, 20, 0, 100), 0, 0));
//		add(new JLabel(), new GridBagConstraints(5, 0, 10, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));

//		ownerLabel = new JLabel(fileEntry.get);
//		groupLabel = new JLabel(fileEntry.get);
//		setPreferredSize(new Dimension(0, 32));
	}

	public FileEntry getFileEntry() {
		return fileEntry;
	}

	public void setFileEntry(FileEntry fileEntry) {
		this.fileEntry = fileEntry;
		
	}
	
	public void setBackground(Color color){
		super.setBackground(color);
	}
	public void setForeground(Color color){
		super.setForeground(color);
		if(nameLabel == null) return;
		nameLabel.setForeground(color);
		permissLabel.setForeground(color);
		sizeLabel.setForeground(color);
		dateLabel.setForeground(color);
		timeLabel.setForeground(color);
	}
	
	
	private ImageIcon getIcon(FileEntry entry){
		return fileEntry.getType() == FileListingService.TYPE_FILE ?
				ResourceUtils.getFileIcon(entry.getName()) : ResourceUtils.getFileIcon(entry.getType());
	}
}
